package com.ltw.playit.web.server.entity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Properties;

import org.apache.commons.lang3.RandomStringUtils;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;

public class IdGenerator implements Configurable, IdentifierGenerator {
	
	private int idLength;
	
	@Override
	public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
		return Calendar.getInstance().getTimeInMillis() + RandomStringUtils.randomNumeric(idLength);
	}

	@Override
	public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
		this.idLength = Integer.parseInt(params.getProperty("idLength")) ;
	}
}
